/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package com.ecarx.notificationx;

import android.app.PendingIntent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.IconCompat;

public class ActionCompat2 {

    @Nullable
    private String text;
    @Nullable
    private Bundle iconBundle;
    @Nullable
    private PendingIntent pendingIntent;
    @Nullable
    private String hint;

    @Nullable
    public String getText() {
        return text;
    }

    @Nullable
    public String getHint() {
        return hint;
    }

    @Nullable
    public IconCompat getIcon() {
        if (iconBundle == null) {
            return null;
        }

        return IconCompat.createFromBundle(iconBundle);
    }

    @Nullable
    public PendingIntent getPendingIntent() {
        return pendingIntent;
    }

    public ActionCompat2(@Nullable String text, @Nullable IconCompat icon, @Nullable PendingIntent pendingIntent) {
        this(text, pendingIntent, icon, null);
    }

    public ActionCompat2(@Nullable String text, @Nullable PendingIntent pendingIntent, @Nullable IconCompat icon, @Nullable String hint) {
        this.text = text;
        if (icon != null) {
            this.iconBundle = icon.toBundle();
        }
        this.pendingIntent = pendingIntent;
        this.hint = hint;
    }

    public static ActionCompat2 fromBundle(@NonNull Bundle bundle) {
        final Bundle icon = bundle.getBundle(KEY_ICON);
        return new ActionCompat2(bundle.getString(KEY_TEXT), (PendingIntent) (bundle.getParcelable(KEY_INTENT)), icon == null ? null : IconCompat.createFromBundle(icon), bundle.getString(KEY_HINT));
    }

    private static final String KEY_TEXT = "text";
    private static final String KEY_HINT = "hint";
    private static final String KEY_ICON = "icon";
    private static final String KEY_INTENT = "intent";

    @NonNull
    public Bundle toBundle() {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TEXT, text);
        bundle.putString(KEY_HINT, hint);
        bundle.putBundle(KEY_ICON, iconBundle);
        bundle.putParcelable(KEY_INTENT, pendingIntent);
        return bundle;
    }

    public ActionCompat2(@NonNull String text, @NonNull PendingIntent pendingIntent) {
        this(text, null, pendingIntent);
    }

    public ActionCompat2(@NonNull String text) {
        this(text, null, null);
    }

}
